package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.OrderCommentDataBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 *
 * @author Administrator
 * @date 2018/4/19
 */

public class OrderCommentModel implements OrderCommentModelImpl{

    private OrderCommentModel.OrderCommentDataResponseListener mListener;

    private static final String TAG = "OrderCommentModel";

    public OrderCommentModel(OrderCommentModel.OrderCommentDataResponseListener listener) {
        mListener = listener;
    }
    public interface OrderCommentDataResponseListener{
        void onGetDetailSuccess(OrderCommentDataBean bean);
        void onGetDetailError(Throwable throwable);

    }

    /**
     *  添加订单评价添加参数
     * @param aid  活动id
     * @param from_uid 用户id
     * @param order_id 订单id
     * @param contents 评价内容
     * @param order_star 货物五星评价
     * @param logistics_star 物流五星评价
     * @param merch_star 活动方五星评价
     * @param anonymous 是否匿名
     * @param imgPaths 评价图片地址list
     */
    @Override
    public void setOrderComment(String aid, String from_uid, String order_id, String contents,
                                String order_star, String logistics_star, String merch_star,
                                 String anonymous, List<String> imgPaths) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("aid", aid)
                .addFormDataPart("from_uid",from_uid)
                .addFormDataPart("order_id",order_id)
                .addFormDataPart("contents",contents)
                .addFormDataPart("order_star",order_star)
                .addFormDataPart("logistics_star",logistics_star)
                .addFormDataPart("merch_star",merch_star)
                .addFormDataPart("is_anonymous",anonymous);
        for (String imgPath : imgPaths) {
            File file = new File(imgPath);
            builder.addFormDataPart(
                    "imgurl[]",
                    file.getName(),
                    RequestBody.create(MediaType.parse("images/*"), file)
            );
        }
        RequestBody requestBody = builder.build();

        Observable<OrderCommentDataBean> observable = DataManager.setOrderCommentData(requestBody);
        Observable<OrderCommentDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

        observeOn.subscribe(new DisposableObserver<OrderCommentDataBean>() {
            @Override
            public void onNext(OrderCommentDataBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }
}
